﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmHothu78 : Form
    {
        public FrmHothu78()
        {
            InitializeComponent();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab==tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Hothu78d;
            }
            else
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Hothu78t;
            }
        }

        private void FrmHothu78_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmHothu78_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(584, 550);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrHothu78 prHothu78 = new PrHothu78();
            prHothu78.Show();
            this.Hide();
        }

        private void FrmHothu78_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            Values = txtCaotuongdauH0.Text;//0
            Values = Values +"\t"+ TxtDaymongtruoc.Text;//1
            Values = Values +"\t"+ txtDaymongsau.Text;//2
            Values = Values +"\t"+ txtLopdem.Text;//3
            Values = Values +"\t"+ TxtSaudongghet.Text;//4
            Values = Values +"\t"+ txtDoclungtuong.Text;//5
            Values = Values +"\t"+ txtDoctuongm.Text;//6
            Values = Values +"\t"+ txtRongmongb.Text;//7
            Values = Values +"\t"+ TxtMorongmong.Text;//8
            Values = Values +"\t"+ optDaymong.Checked.ToString();//9
            Values = Values +"\t"+ optMomong.Checked.ToString();//10
            Values = Values +"\t"+ TxtRongHt.Text;//11
            Values = Values +"\t"+ txtDaylongh2.Text;//12
            Values = Values +"\t"+ txtDaythanhbend1.Text;//13
            Values = Values +"\t"+ TxtDaythanhcuoid2.Text;//14
            Values = Values +"\t"+ TxtGocmoa1.Text;//15
            Values = Values +"\t"+ txtNghiengthanhcuoi.Text;//16
            Values = Values +"\t"+ txtMuongH1.Text;//17
            Values = Values +"\t"+ txtMuongb1.Text;//18
            Values = Values +"\t"+ txtMuongb2.Text;//19
            Values = Values +"\t"+ txtMuongb3.Text;//20
            Values = Values +"\t"+ txtMuongdoc.Text;//21
            regKey.SetValue("KtHothu78", Values);
            regKey.Close();
        }
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtHothu78").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                txtCaotuongdauH0.Text=STR[i++];//0
                TxtDaymongtruoc.Text = STR[i++];//1
                txtDaymongsau.Text = STR[i++];//2
                txtLopdem.Text = STR[i++];//3
                TxtSaudongghet.Text = STR[i++];//4
                txtDoclungtuong.Text = STR[i++];//5
                txtDoctuongm.Text = STR[i++];//6
                txtRongmongb.Text = STR[i++];//7
                TxtMorongmong.Text = STR[i++];//8
                optDaymong.Checked = bool.Parse(STR[i++]);//9
                optMomong.Checked = bool.Parse(STR[i++]);//10
                TxtRongHt.Text = STR[i++];//11
                txtDaylongh2.Text = STR[i++];//12
                txtDaythanhbend1.Text = STR[i++];//13
                TxtDaythanhcuoid2.Text = STR[i++];//14
                TxtGocmoa1.Text = STR[i++];//15
                txtNghiengthanhcuoi.Text = STR[i++];//16
                txtMuongH1.Text = STR[i++];//17
                txtMuongb1.Text = STR[i++];//18
                txtMuongb2.Text = STR[i++];//19
                txtMuongb3.Text = STR[i++];//20
                txtMuongdoc.Text = STR[i++];//21
                regKey.Close();
            }
        }
        // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.htv)|*.htv";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Ho thu
                Str = "------DAU CONG------" +"\r\n";
                Str = Str + "Cao tuong dau H0:" +"\t"+ txtCaotuongdauH0 .Text +"\r\n";
                Str = Str + "Cao tuong dau H1:" +"\t"+ txtCaotuongdauH1 .Text +"\r\n";
                Str = Str + "Day mong truoc:" +"\t"+ TxtDaymongtruoc .Text +"\r\n";
                Str = Str + "Day mong sau:" +"\t"+ txtDaymongsau .Text +"\r\n";
                Str = Str + "Day lop dem lot H4:" +"\t"+ txtLopdem .Text +"\r\n";
                Str = Str + "Sau dong ghet H5:" +"\t"+ TxtSaudongghet .Text +"\r\n";
                Str = Str + "Doc lung tuong i:" +"\t"+ txtDoclungtuong .Text +"\r\n";
                Str = Str + "Doc dau cong:" +"\t"+ txtDoctuongm .Text +"\r\n";
                Str = Str + "Rong mong :" +"\t"+ txtRongmongb .Text +"\r\n";
                Str = Str + "Mo rong mong :" +"\t"+ TxtMorongmong .Text +"\r\n";
                Str = Str + "Chon rong mong:" +"\t"+ optDaymong.Checked.ToString() +"\r\n";
                Str = Str + "Chon mo rong mong:" +"\t"+ optMomong.Checked.ToString() +"\r\n";
                //Muong doc
                Str = Str + "------HO THU------" +"\r\n";
                Str = Str + "Be rong doc B:" +"\t"+ TxtRongHt .Text +"\r\n";
                Str = Str + "Be rong ngang Bn:" +"\t"+ TxtNgangHt .Text +"\r\n";
                Str = Str + "Cao ho thu:" +"\t"+ TxtCaoHt .Text +"\r\n";
                Str = Str + "Day long:" +"\t"+ txtDaylongh2 .Text +"\r\n";
                Str = Str + "Day thanh ben:" +"\t"+ txtDaythanhbend1 .Text +"\r\n";
                Str = Str + "Day thanh cuoi:" +"\t"+ TxtDaythanhcuoid2 .Text +"\r\n";
                Str = Str + "Goc mo tren:" +"\t"+ TxtGocmoa1 .Text +"\r\n";
                Str = Str + "Goc mo duoi:" +"\t"+ TxtGocmoa2 .Text +"\r\n";
                Str = Str + "Nghieng thanh cuoi:" +"\t"+ txtNghiengthanhcuoi .Text +"\r\n";
                Str = Str + "Bo tri muong doc:" +"\t"+ txtMuongdoc .Text +"\r\n";
                Str = Str + "Sau long muong H1:" +"\t"+ txtMuongH1 .Text +"\r\n";
                Str = Str + "Cao dinh muong H2:" +"\t"+ txtMuongH2 .Text +"\r\n";
                Str = Str + "Rong thanh vai b1:" +"\t"+ txtMuongb1 .Text +"\r\n";
                Str = Str + "Rong vai muong b2:" +"\t"+ txtMuongb2 .Text +"\r\n";
                Str = Str + "Rong long muong b3:" +"\t"+ txtMuongb3 .Text +"\r\n";

                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
              // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.htv)|*.htv";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //DAU CONG
                txtCaotuongdauH0.Text = Values[i].Split('\t')[1]; i++;
                txtCaotuongdauH1.Text = Values[i].Split('\t')[1]; i++;
                TxtDaymongtruoc.Text = Values[i].Split('\t')[1]; i++;
                txtDaymongsau.Text = Values[i].Split('\t')[1]; i++;
                txtLopdem.Text = Values[i].Split('\t')[1]; i++;
                TxtSaudongghet.Text = Values[i].Split('\t')[1]; i++;
                txtDoclungtuong.Text = Values[i].Split('\t')[1]; i++;
                txtDoctuongm.Text = Values[i].Split('\t')[1]; i++;
                txtRongmongb.Text = Values[i].Split('\t')[1]; i++;
                TxtMorongmong.Text = Values[i].Split('\t')[1]; i++;
                optDaymong.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                optMomong.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                //HO THU
                i = i + 1;
                TxtRongHt.Text = Values[i].Split('\t')[1]; i++;
                TxtNgangHt.Text = Values[i].Split('\t')[1]; i++;
                TxtCaoHt.Text = Values[i].Split('\t')[1]; i++;
                txtDaylongh2.Text = Values[i].Split('\t')[1]; i++;
                txtDaythanhbend1.Text = Values[i].Split('\t')[1]; i++;
                TxtDaythanhcuoid2.Text = Values[i].Split('\t')[1]; i++;
                TxtGocmoa1.Text = Values[i].Split('\t')[1]; i++;
                TxtGocmoa2.Text = Values[i].Split('\t')[1]; i++;
                txtNghiengthanhcuoi.Text = Values[i].Split('\t')[1]; i++;
                txtMuongdoc.Text = Values[i].Split('\t')[1]; i++;
                txtMuongH1.Text = Values[i].Split('\t')[1]; i++;
                txtMuongH2.Text = Values[i].Split('\t')[1]; i++;
                txtMuongb1.Text = Values[i].Split('\t')[1]; i++;
                txtMuongb2.Text = Values[i].Split('\t')[1]; i++;
                txtMuongb3.Text = Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmHothu78_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
